package com.qzh.G.base;

import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 容器
 */
@Data
public final class ActiveObjectContainer {
    private static Map<Long,ActiveObject>map;
    private static ActiveObjectContainer ActiveObjectContainer;

    private  ActiveObjectContainer(){
        map = new HashMap<>();
    }
   public static ActiveObjectContainer getInstance() {
       if (ActiveObjectContainer == null) {
           ActiveObjectContainer =new ActiveObjectContainer();
       }
       return ActiveObjectContainer;
    }
    public static void addObject(ActiveObject activeObject){
        if(!map.containsKey(activeObject.getId())){
            map.put(activeObject.getId(), activeObject);
        }
    }
    public static void removeObject(long id){
        if(map.containsKey(id)&&id!= 1L){
            map.remove(id);
        }
    }
    public static ActiveObject getPlayer(){
        return map.get(1);
    }
    public static ActiveObject getById(long id){  return map.get(id);}

    public static ActiveObject getByName(String name){
        List<ActiveObject> list = map.values().stream().filter(activeObject -> {
            return activeObject.getName().equals(name);
        }).collect(Collectors.toList());
        return list.get(0);
    }

    public void dealAttack(Attack attack) {
        long fromid = attack.getFrom();
        long toid = attack.getTo();
            if(map.containsKey(toid)){
                map.get(toid).getHurt(attack);
            }
    }
    private void printer(){}

}
